/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package atrevaluator;

import java.io.File;
import java.util.ArrayList;

/**
 * The document converter requires OpenOffice to be installed and they must
 * be running as a service. This can be achieved by:
 * 
 * 
 * @author pk3295
 */
public class ReadDirectory {

    private String indent = "";
    private File originalFileObject;
    private File fileObject;
    private ArrayList<String> files;
    private ArrayList<String> allowedExtensions;
    
    public ReadDirectory(String path) {
        this.fileObject = new File(path);        
        this.originalFileObject = this.fileObject;
        this.files = new ArrayList<String>();
        this.allowedExtensions = new ArrayList<String>();        
    }
    
    public ArrayList<String> getRecursiveList() {
        
        return this.recursiveTraversal(this.fileObject);        
        
    }    
    
    public ArrayList<String> recursiveTraversal(File fileObject) {
        
        if (fileObject.isDirectory()){
            //indent = getIndent(fileObject);
            //System.out.println(indent +  fileObject.getName());

            File allFiles[] = fileObject.listFiles();
            for(File aFile : allFiles){
                recursiveTraversal(aFile);
        }
        }else if (fileObject.isFile()){
            
            // Accept only the extensions that we are able to convert using
            // JODConverter            
            if (this.checkExtension(fileObject)) {       
                                
                files.add(fileObject.getAbsolutePath());
                
            } else {
                //System.out.println("Refusing: " + fileObject.getName());
            }
            //System.out.println(fileObject.getAbsolutePath());
            //System.out.println(indent  + "  " + fileObject.getName());
        }
        
        return files;
    }
       
    private String getIndent(File fileObject)
    {
        String original = originalFileObject.getAbsolutePath();
        String fileStr = fileObject.getAbsolutePath();		
        String subString = 
            fileStr.substring(original.length(), fileStr.length());

        String myIndent = ""; 
        for(int index=0; index < subString.length(); index ++){
            char aChar = subString.charAt(index);
            if (aChar == File.separatorChar){
                myIndent = myIndent + "  ";
            }
        }
    return myIndent;
    }

    private String getExtension(File fileObject) {
        
        int lastDot = fileObject.getName().lastIndexOf('.');
        //Dot not found
        if (lastDot != -1) {
            return fileObject.getName().substring(lastDot).toLowerCase();
        } else {
            return "";
        }
    }
    
    private boolean checkExtension(File fileObject) {
        
        // if no extension is set, than do not filter
        if (this.allowedExtensions.size() == 0) {
            return true;
        }
        
        boolean accepted = false;
        int i = 0;
        while (!accepted && (i < this.allowedExtensions.size())) {
            accepted = (this.getExtension(fileObject).equals(this.allowedExtensions.get(i)));
            i ++;
        }        
        
        return accepted;
    }
    
   private String trimExtension (String filename) {
       int lastDot = filename.lastIndexOf('.');
        //Dot not found
        if (lastDot != -1) {
            return filename.substring(0, lastDot);
        } else {
            return "";
        }       
   }
   
   public void addExtension(String extension) {
      this.allowedExtensions.add(extension);
   }
    
}
